package com.example.wsm.controller;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wsm.common.QueryPageParam;
import com.example.wsm.common.Result;
import com.example.wsm.entity.Goods;
import com.example.wsm.entity.Storage;
import com.example.wsm.service.impl.GoodsService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wms
 * @since 2024-06-20
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService GoodsService;

    // 删除
    @GetMapping("/del")
    public Result del(@RequestParam String id) {
        return GoodsService.removeById(id) ? Result.suc() : Result.fail();
    }

    // 新增
    @PostMapping("/save")
    public Result save(@RequestBody Goods Goods) {
        return GoodsService.save(Goods) ? Result.suc() : Result.fail();
    }

    // 更新
    @PostMapping("/update")
    public Result update(@RequestBody Goods Goods) {
        return GoodsService.updateById(Goods) ? Result.suc() : Result.fail();
    }

    @PostMapping("/listPage")
    // public List<Goods> listPage(@RequestBody HashMap map) {
    public Result listPageF(@RequestBody QueryPageParam query) {
        // query.get("pageSize");
        HashMap param = query.getParam();
        String name = (String) param.get("name");
        String goodstype = (String) param.get("goodstype");
        String storage = (String) param.get("storage");
        Page<Goods> page = new Page<Goods>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name) && !"null".equals((name))) {
            lambdaQueryWrapper.like(Goods::getName, name);
        }
        if (StringUtils.isNotBlank(storage) && !"null".equals((name))) {
            lambdaQueryWrapper.eq(Goods::getStorage, storage);
        }
        if (StringUtils.isNotBlank(goodstype) && !"null".equals((name))) {
            lambdaQueryWrapper.eq(Goods::getGoodstype, goodstype);
        }
        IPage<Goods> result = GoodsService.page(page, lambdaQueryWrapper);
        return Result.suc(result.getTotal(), result.getRecords());
    }
}
